package com.itzdm.reggie.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itzdm.reggie.common.Result.Result;
import com.itzdm.reggie.dto.EmployeeDTO;
import com.itzdm.reggie.entity.Employee;
import com.itzdm.reggie.service.EmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/employee")
@Slf4j
@Api(tags = "员工相关接口")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;


    /**
     * 员工登录接口
     *
     * @param request
     * @param employee
     * @return
     */
    @PostMapping("/login")
    @ApiOperation("员工登录接口")
    public Result<Employee> login(HttpServletRequest request, @RequestBody Employee employee) {
        log.info("用户登录：{}",employee);
        Employee emp = employeeService.login(request,employee);
        return Result.success(emp);
    }


    /**
     * 员工登出接口
     *
     * @param request
     * @return
     */
    @PostMapping("/logout")
    @ApiOperation("员工登出接口")
    public Result<String> logout(HttpServletRequest request) {
        log.info(request.getSession().getAttribute("employee") + "员工登出");
        request.getSession().removeAttribute("employee");
        return Result.success("退出成功");
    }


    /**
     * 新增员工接口
     * @return
     */
    @PostMapping
    @ApiOperation("新增员工接口")
    public Result<String> save(HttpServletRequest request,@RequestBody EmployeeDTO employeeDTO){
        log.info("新增员工：{}", employeeDTO);
        employeeService.saveEmp(request,employeeDTO);
        return Result.success("添加员工成功");
    }

    /**
     * 分页查询员工接口
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("分页查询员工接口")
    public Result<Page> page(int page, int pageSize,String name){
        log.info("分页查询员工：{}{}{}",page,pageSize,name);
        Page p = employeeService.pageEmp(page,pageSize,name);
        return Result.success(p);
    }

    /**
     * 修改员工状态接口
     * @param employee
     * @return
     */
    @PutMapping
    @ApiOperation("修改员工状态接口")
    public Result<String> startOrStopStatus(HttpServletRequest request, @RequestBody Employee employee){
        log.info("修改员工账号状态:{}",employee);
        employeeService.updateStatus(request,employee);
        return Result.success("更改状态成功");
    }


    /**
     * 根据id获得员工信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id获得员工信息")
    public Result<Employee> getEmpById(@PathVariable Long id){
        log.info("根据id获得员工信息：{}",id);
        Employee employee = employeeService.getById(id);
        if (employee != null){
            return Result.success(employee);
        }
        return Result.error("未查询到对应员工信息");
    }
}
